#pragma once
#include <iostream>
#include <assert.h>
//#include<vector>
//#include <unordered_map>

using namespace std;

namespace WX
{
	template <class T>
	class vector
	{

	public :

		typedef T* iterator;
		typedef const T* const_iterator;

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}

		vector()
		{}

		vector(int n, const T& x = T())
		{
			resize(n, x);
		}

		vector(size_t n, const T& x = T())
		{
			resize(n, x);
		}

		vector(initializer_list<T> il)
		{
			reserve(il.size());
			for (auto& e : il)
			{
				push_back(e);
			}
		}

		vector(const vector<T>& v)
		{
			reserve(v.size());
			for (auto& e : v)
			{
				push_back(e);
			}
		}

		void resize(size_t n,const T& val = T())
		{
			if (size() > n)
			{
				_finish = _start + n;
			}
			else
			{
				reserve(n);
				while (_finish != _start + n)
				{
					*_finish = val;
					_finish++;
				}
			}
		}

		template <class InputTterator>
		vector(InputTterator first, InputTterator last)
		{
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}



		//vector<T>& operator=(const vector<T>& v)
		//{
		//	if (this != &v)
		//	{
		//		delete[] _start;
		//		_finish = _end_of_storage = nullptr;

		//		reserve(v.size());
		//		for (auto e : v)
		//		{
		//			push_back(e);
		//		}
		//	}

		//	return *this;
		//}

		void swap(vector<T>& v)
		{
			std::swap(_start, v._start);
			std::swap(_finish, v._finish);
			std::swap(_end_of_storage, v._end_of_storage);
		}

		vector<T>& operator=( vector<T> v)
		{
			swap(v);
			return *this;
		}


		T& operator[](size_t i)
		{

			return _start[i];
		}

		void pop_back()
		{
			assert(_start != _finish);
			_finish--;
		}

		void insert(iterator pos,const T& x)
		{
			assert(pos >= _start);
			assert(pos <= _finish);

			size_t len = pos - _start;
			reserve(size() + 1);
			pos = _start + len;

			iterator it = _finish - 1;
			while (pos <= it)
			{
				*(it + 1) = *it;
				it--;
			}
			*pos = x;
			_finish++;
		}

		iterator earse(iterator pos)
		{
			assert(pos >= _start);
			assert(pos <= _finish);

			iterator it = pos + 1;
			while (it < _finish)
			{
				*(it - 1) = *it;
				it++;
			}

			_finish;
			return pos;
		}

		void reserve(size_t n)
		{
			if (capacity() < n)
			{
				size_t Oldsize = size();
				T* tmp = new T[n];
				memcpy(tmp, _start, sizeof(T)*Oldsize);
				delete[] _start;

				_start = tmp;
				_finish = tmp + Oldsize;
				_end_of_storage = tmp + n;
			}
		}

		void push_back(const T& x)
		{
			if (size() == capacity())
			{
				reserve(capacity() == 0 ? 4 : capacity() * 2);
			}
			*_finish = x;
			_finish++;
		}

		~vector()
		{
			delete[] _start;
			_finish = nullptr;
			_end_of_storage = nullptr;
		}

		size_t size()
		{
			return _finish - _start;
		}

		size_t capacity()
		{
			return _end_of_storage - _start;
		}

		size_t size() const
		{
			return _finish - _start;
		}

		size_t capacity() const
		{
			return _end_of_storage - _start;
		}

	private:
		T* _start = nullptr;
		T* _finish = nullptr;
		T* _end_of_storage = nullptr;
	};
}

